package com.txc.books.controller;


import com.txc.books.entity.Book;
import com.txc.books.exception.BaseException;
import com.txc.books.service.IBookService;
import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import org.springframework.web.bind.annotation.*;



/**
 * 图书控制器
 * @author: XCTANG
 */

@RestController
@RequestMapping("/book")
public class BookController {

    private final IBookService bookService;

    public BookController(IBookService bookService){
        this.bookService = bookService;
    }

    //查找全部图书
    @GetMapping(value = "/findAll")
    public void findAll(){
        bookService.findAll();
    }

    //添加图书信息
    @PostMapping(value = "/insert")
    public void addBook(Book book){
        try{
            bookService.insert(book);
        }catch (BaseException e){
            System.out.println("出现定义的异常");
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }

    //更新图书信息
    @PostMapping(value = "/update")
    public void updateBook(Book book){

        try{
            bookService.update(book);
        }catch (BaseException e){
            System.out.println("出现定义的异常");
        }catch (Exception e){
            System.out.println("出现不可预料的异常");
        }
    }


    //删除图书信息
    @GetMapping(value = "/delete")
    public void deleteBook(@RequestParam("id") String id){
        bookService.delete(Integer.parseInt(id.trim()));
    }

    //根据id查询图书信息
    @GetMapping(value = "/select")
    public void select(@RequestParam("id") String id){
        bookService.select(Integer.parseInt(id));
    }

    @PostMapping(value="/findPage")
    public PageResult findPage(@RequestBody PageRequest pageQuery) {

        return bookService.findByPager(pageQuery);
    }

}
